package com.coldworks.coldjoke.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.model.JokeModel;
import com.coldworks.coldjoke.model.ReplyModel;
import com.coldworks.coldjoke.model.TopicModel;

public class JsonUtil {
	protected final static Logger log = LoggerFactory.getLogger(JsonUtil.class);

	public JsonUtil() {

	}

	// 获取一个笑话内容，包括该笑话评论
	public static JokeModel getJokeInfo(JSONObject jsonObject) {
		JokeModel jokeInfo = new JokeModel();
		try {
			jokeInfo.setJokeId(jsonObject.getString("Jokeid"));
			jokeInfo.setTime(jsonObject.getString("created_cn"));
			jokeInfo.setJokeText(jsonObject.getString("content"));
			String imgUrl = jsonObject.getString("uri");
			if (!imgUrl.equals("") && !imgUrl.startsWith("http")) {
				imgUrl = CONST.URL.HOST + imgUrl;
			}
			jokeInfo.setImgUrl(imgUrl);
			jokeInfo.setGoodNum(jsonObject.getInt("like_count"));
			jokeInfo.setBadNum(jsonObject.getInt("unlike_count"));
			jokeInfo.setReplyNum(jsonObject.getInt("comment_count"));
			jokeInfo.setUserId(jsonObject.getString("Userid"));
			jokeInfo.setUserName(jsonObject.getString("user_name"));
			String iconUrl = jsonObject.getString("user_cover_url");
			if (!iconUrl.equals("") && !iconUrl.startsWith("http")) {
				iconUrl = CONST.URL.HOST + iconUrl;
			}
			jokeInfo.setUserIcon(iconUrl);
		} catch (Exception e) {
			log.error("", e);
			return null;
		}

		return jokeInfo;
	}

	// 获取一个笑话列表
	public static List<JokeModel> getJokeInfoList(JSONObject jsonResult) {
		JSONArray jsonArray = null;
		JokeModel jokeInfo = null;
		List<JokeModel> listTemp = null;
		try {
			jsonArray = jsonResult.getJSONArray("jokes");
			listTemp = new ArrayList<JokeModel>();
			for (int i = 0; i < jsonArray.length(); i++) {
				jokeInfo = new JokeModel();
				jokeInfo = getJokeInfo(jsonArray.getJSONObject(i));
				listTemp.add(jokeInfo);
			}
		} catch (JSONException e) {
			log.error("", e);
			return null;
		}
		return listTemp;
	}

	public static ReplyModel getReplyInfo(JSONObject jsonObject) {

		ReplyModel replyInfo = new ReplyModel();
		try {
			JSONObject author = new JSONObject(jsonObject.getString("author"));
			replyInfo.setUserName(author.getString("username"));
			replyInfo.setUserId(jsonObject.getString("Userid"));
			replyInfo.setReplyContent(jsonObject.getString("content"));

		} catch (Exception e) {
			log.error("", e);
			return null;
		}

		return replyInfo;
	}

	public static List<ReplyModel> getReplyInfoList(JSONArray jsonArray) {
		ReplyModel replyInfo = null;
		List<ReplyModel> list = null;
		try {
			list = new ArrayList<ReplyModel>();
			for (int i = 0; i < jsonArray.length(); i++) {
				replyInfo = new ReplyModel();
				replyInfo = getReplyInfo(jsonArray.getJSONObject(i));
				list.add(replyInfo);
			}
		} catch (JSONException e) {
			log.error("", e);
			return null;
		}
		return list;
	}

	public static List<TopicModel> getTopicInfoList(JSONObject jsonResult) {
		JSONArray jsonArray = null;
		TopicModel topicInfo = null;
		List<TopicModel> list = null;
		try {
			jsonArray = jsonResult.getJSONArray("gathers");
			list = new ArrayList<TopicModel>();
			for (int i = 0; i < jsonArray.length(); i++) {
				topicInfo = getTopicInfo(jsonArray.getJSONObject(i));
				list.add(topicInfo);
			}
		} catch (JSONException e) {
			log.error("", e);
			return null;
		}
		return list;
	}

	public static TopicModel getTopicInfo(JSONObject jsonObject) {
		TopicModel topicInfo;
		try {
			topicInfo = new TopicModel();
			topicInfo.setGatherId(jsonObject.getString("Gatherid"));
			topicInfo.setSummary(jsonObject.getString("summary"));
			String url = jsonObject.getString("uri");
			if (!url.equals("") && !url.startsWith("http")) {
				url = CONST.URL.HOST + url;
			}
			topicInfo.setImgUrl(url);
			topicInfo.setReplyNum(jsonObject.getInt("comment_count"));
			topicInfo.setType(jsonObject.getString("type"));
			topicInfo.setTitle(jsonObject.getString("title"));
			topicInfo.setTimeCreated(jsonObject.getString("created_cn"));
		} catch (Exception e) {
			log.error("", e);
			return null;
		}

		return topicInfo;
	}

}
